﻿using System;

namespace Wisdom.Core.Entities
{
    /// <summary>
    /// 实体基类
    /// </summary>
    [Serializable]
    public abstract class Entity : IEntity
    {
        #region Implementation of IEntity

        /// <summary>
        /// Gets the Id of the entity.
        /// </summary>
        public Guid Id { get; set; }

        #endregion

        /// <summary>
        /// 重写Equals方法，只要实体ID相等，就表示相等
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj))
                return true;
            if (obj == null)
                return false;

            var other = obj as IEntity;
            if (other == null)
                return false;

            return other.Id != Guid.Empty && this.Id != Guid.Empty && other.Id == this.Id;
        }
        /// <summary>
        /// 重写GetHashCode方法，返回实体ID的HashCode
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.Id.GetHashCode();
        }

        /// <summary>
        /// 确定两个实体是否相等
        /// </summary>
        /// <param name="entity1"></param>
        /// <param name="entity2"></param>
        /// <returns></returns>
        public static bool operator ==(Entity entity1, Entity entity2)
        {
            if (Object.ReferenceEquals(entity1, null) || Object.ReferenceEquals(entity2, null))
            {
                return Object.ReferenceEquals(entity1, entity2);
            }
            return entity1.Equals(entity2);
        }

        /// <summary>
        /// 确定两个实体是否不相等等
        /// </summary>
        /// <param name="entity1"></param>
        /// <param name="entity2"></param>
        /// <returns></returns>
        public static bool operator !=(Entity entity1, Entity entity2)
        {
            return !(entity1 == entity2);
        }
    }
}